<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      chartsUnstance: null,
      allData: null, // 返回的数据
      timeId: null, // 定时器的唯一标识
      currentIndex: 0 // 当前显示的数据页数
    }
  },

  created () {
    // 在组件创建完成之后,进行回调函数的注册
    this.$socket.registerCallBack('stockData', this.getData)
  },

  mounted () {
    this.initChart()
    // this.getData()

    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
    })

    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },

  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timeId)

    // 在组件销毁的时候,进行回调函数的取消
    this.$socket.unRegisterCallBack('trendData')
  },

  computed: {
    ...mapState(['theme'])
  },

  watch: {
    theme () {
      console.log('主题切换了')
      this.chartsUnstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.undateChart() // 更新图表的展示
    }
  },

  methods: {
    // 初始化图表
    initChart () {
      // 在ref为seller_ref的容器中显示echarts图表
      this.chartsUnstance = this.$echarts.init(
        this.$refs.seller_ref,
        this.theme
      )

      // 关于初始化的图表配置
      const initOption = {
        title: {
          text: '| 库存和销量分析',
          left: 25,
          top: 25
        }
      }

      // 挂载echarts图表
      this.chartsUnstance.setOption(initOption)

      // 鼠标移入事件监听
      this.chartsUnstance.on('mouseover', () => {
        clearInterval(this.timeId)
      })

      // 鼠标移出事件监听
      this.chartsUnstance.on('mouseout', () => {
        this.startInterval()
      })
    },

    // 获取数据
    getData (res) {
      // 发送请求
      // const { data: res } = await this.$Axios.get('stock')

      // 赋值
      this.allData = res

      this.undateChart()
      this.startInterval()
    },

    // 更新图表
    undateChart () {
      // 环形图的坐标
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]

      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]

      // 处理返回的数据
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          center: centerArr[index],
          hoverAnimation: false,
          labelLine: {
            show: false
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          },
          data: [
            {
              value: item.sales,
              name: item.name + '\n\n' + item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })

      // 关于数据的图表配置
      const dataOption = {
        series: seriesArr
      }

      // 挂载echarts图表
      this.chartsUnstance.setOption(dataOption)
    },

    // 屏幕适配
    screenAdapter () {
      // 计算适配的大小
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6
      const innerRadius = titleFontSize * 2.8
      const outterRadius = innerRadius * 1.125

      // 关于适配的图表配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.8
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.8
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.8
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.8
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 1.8
            }
          }
        ]
      }

      // 挂载echarts图表
      this.chartsUnstance.setOption(adapterOption)
      this.chartsUnstance.resize()
    },

    // 计时器
    startInterval () {
      if (this.timeId) {
        clearInterval(this.timeId)
      }
      this.timeId = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }

        this.undateChart()
      }, 4000)
    }
  }
}
</script>

<style lang="less" scoped></style>
